package org.example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class Stream_Demo2 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张三", "李四", "王五", "张六财", "张七望", "孙八");
        list.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                //true 保留
                //false 过滤
                return s.startsWith("张") && s.length() == 3;
            }
        }).forEach(s -> System.out.println(s));
        System.out.println("------------------1");
        //lambda表达式
        list.stream().filter(s -> s.startsWith("张") && s.length() == 3).forEach(s -> System.out.println(s));
        System.out.println("------------------2");
        //limit 限制前面几个元素
        list.stream().limit(3).forEach(s -> System.out.println(s));
        System.out.println("------------------3");
        //skip 跳过前面几个元素
        list.stream().skip(3).forEach(s -> System.out.println(s));
        System.out.println("------------------4");
        //distinct 去重 依赖hashCode和equals方法
        list.stream().distinct().forEach(s -> System.out.println(s));
        System.out.println("------------------5");
        //concat 合并
        ArrayList<Integer> list2 = new ArrayList<>();
        Collections.addAll(list2, 1, 2, 3, 4, 5);
        Stream.concat(list.stream(), list2.stream()).forEach(s -> System.out.println(s));
    }
}
